//
//  XS_DownloadingVC.swift
//  XueDao
//
//  Created by Mac on 2020/12/29.
//  下载中页面

import UIKit
import AliyunMediaDownloader

class XS_DownloadingVC: XS_BaseVC {
    
    lazy var model = XS_DownloadModel()
    
    var foldDataArray = [Bool]()

    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = "下载中"
        
        for _ in model.hourModels {
            foldDataArray.append(false)
        }

        // Do any additional setup after loading the view.
        shouldNavBarHidden = false
        tableView.register(UINib.init(nibName: "XS_DownloadStartListCell", bundle: Bundle.main), forCellReuseIdentifier: "XS_DownloadStartListCell")
        tableView.register(UINib.init(nibName: "XS_DownloadStartHeaderView", bundle: Bundle.main), forHeaderFooterViewReuseIdentifier: "XS_DownloadStartHeaderView")
        
        let view = Bundle.main.loadNibNamed("XS_DownloadFooterView", owner: nil, options: nil)?.last as! XS_DownloadFooterView
        let filePath = XS_DownloadManager_CourseFilePath
        let fileSize = filePath.getFileSize()
        let freeSize = XS_DownloadManager.shared.getSystemFreeSize()
        view.titleLabel.text = String.init(format: "已下载课件%ldM ,可用空间%ldG", fileSize/1024/1024,freeSize/1024/1024/1024)
        tableView.tableFooterView = view
        
        tableView.reloadData()
        
        NotificationCenter.default.addObserver(self, selector: #selector(updateProgress(noti:)), name: NSNotification.Name.init(XS_DownloadManager_ProgressNoti), object: nil)
    }
    
    @objc func updateProgress(noti:Notification){
        let percent = noti.object
        for hourModel in model.hourModels {
            for listModel in hourModel.curriculumHourDtoList {
                if (XS_DownloadManager.shared.currentModel?.listModel.id == listModel.id && XS_DownloadManager.shared.currentModel?.listModel.curriculumHourName == listModel.curriculumHourName) {
                    listModel.progress = percent as! Int32
                    if listModel.progress == 100 {
                        listModel.isDownloading = 0
                    }
                }
            }
        }
        tableView.reloadData()
    }
}

extension XS_DownloadingVC : UITableViewDelegate,UITableViewDataSource{
    func numberOfSections(in tableView: UITableView) -> Int {
        return model.hourModels.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        let hourModel = model.hourModels[section]
        let ret = foldDataArray[section]
        return ret ? 0 : hourModel.curriculumHourDtoList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "XS_DownloadStartListCell", for: indexPath) as! XS_DownloadStartListCell
        if model.hourModels.count > 0 {
            let hourModel = model.hourModels[indexPath.section]
            let dtoModel = hourModel.curriculumHourDtoList[indexPath.row]
            dtoModel.isManage = false
            cell.model = dtoModel
        }
        cell.isShow = (cell.model.isDownloading == 1 && cell.model.progress != 100)
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let sectionModel = model.hourModels[indexPath.section]
        let rowModel = sectionModel.curriculumHourDtoList[indexPath.row]
        return (rowModel.isDownloading == 1 && rowModel.progress != 100) ? 64 : 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerView = Bundle.main.loadNibNamed("XS_DownloadStartHeaderView", owner: nil, options: nil)?.last as! XS_DownloadStartHeaderView
        headerView.delegate = self
        headerView.section = section
        headerView.model = model.hourModels[section]
        return headerView
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        let sectionModel = model.hourModels[section]
        var rowCount = 0
        for rowModel in sectionModel.curriculumHourDtoList {
            if (rowModel.isDownloading == 1 && rowModel.progress != 100) {
                rowCount = rowCount + 1
            }
        }
        return rowCount > 0 ? 54 : 0
    }
}

extension XS_DownloadingVC : XS_DownloadStartHeaderViewDelegate{
    func headerView(headerView: XS_DownloadStartHeaderView, didClickFoldBtn: QMUIButton) {
        didClickFoldBtn.isSelected = !didClickFoldBtn.isSelected
        headerView.model.selected = didClickFoldBtn.isSelected
        foldDataArray[headerView.section] = didClickFoldBtn.isSelected
        tableView.reloadData()
    }
}
